/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef OLA_COAP_H
#define OLA_COAP_H
#include "ola_coap_def.h"
#include "ola_sdk_api.h"
#include "ola_error.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
    OlaCoapFrameType frameType;
    OlaCoapMsgType msgType;
    unsigned char code;
    unsigned char tokenLen;
    unsigned char token[OLA_COAP_TOKEN_MAX_LEN];
    unsigned short msgId;
    unsigned char optionsNum;
    OlaCoapOption options[OLA_COAP_OPTION_MAX_NUM];
    OlaData payload;
} OlaCoapPacketParam;

/* 根据缓冲区数据data解码出coap结构体，pkt会使用data内的数据指针 */
int OlaCoapDecode(OlaCoapPacket *pkt, const OlaData *data, OlaCoapFrameType type);

/* 根据coap参数构造coap报文buf以及相应的结构体pkt */
int OlaCoapEncode(OlaCoapPacket *pkt, const OlaCoapPacketParam *param, OlaBuffer *buf);

/* 根据coap参数计算coap报文大小 */
int OlaCoapGetSize(const OlaCoapPacketParam *param, unsigned int *size);

/* 拼接coap的URI */
int OlaCoapGetUri(const OlaCoapPacket *pkt, OlaBuffer *buf);

void OlaCoapDumpPacket(const OlaCoapPacket *pkt);

/*
 * 功能: coap over tcp需要使用coap报文头部的几个字节来确定报文长度
 * 参数: header，报文首字节指针
 *       len，报文头部字节长度
 *       size，出参，计算出的coap报文长度
 * 返回: OLA_OK，计算成功
 *       非OLA_OK，计算失败
 */
int OlaGetTcpCoapPacketSizeByHeader(const unsigned char *header, unsigned int len, unsigned int *size);

#ifdef __cplusplus
}
#endif

#endif /* OLA_COAP_H */